//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.liy.framework.common.office;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OpenOfficeUtil {
    protected static Logger log = LoggerFactory.getLogger(OpenOfficeUtil.class);
    private static final String[] OFFICE_POSTFIXS = new String[]{"doc", "docx", "xls", "xlsx", "ppt", "pptx"};
    private ArrayList<String> Office_Formats = new ArrayList();
    private DefaultOfficeManagerConfiguration config;
    private OfficeManager officeManager;
    private OfficeDocumentConverter converter;

    public OpenOfficeUtil(String oPENOFFICE_HOME) {
        Collections.addAll(this.Office_Formats, OFFICE_POSTFIXS);
        this.config = new DefaultOfficeManagerConfiguration();
        this.config.setOfficeHome(oPENOFFICE_HOME);
        log.info("OpenOffice.Home: " + oPENOFFICE_HOME);
        this.officeManager = this.config.buildOfficeManager();
        this.officeManager.start();
        this.converter = new OfficeDocumentConverter(this.officeManager);
    }

    public void stopOfficeManager() {
        if (this.officeManager != null) {
            this.officeManager.stop();
        }

    }

    public boolean convert2Pdf(String inputFilePath, String outputFilePath) throws FileNotFoundException {
        long begin_time = (new Date()).getTime();
        boolean flag = false;
        log.info("转换开始...");
        File inputFile = new File(inputFilePath);
        if (null != inputFilePath && inputFile.exists()) {
            String postfix = this.getPostfix(inputFilePath);
            if (this.Office_Formats.contains(postfix)) {
                File outputFile = new File(null == outputFilePath ? this.generateDefaultOutputFilePath(inputFilePath) : outputFilePath);
                if (!outputFile.getParentFile().exists()) {
                    outputFile.getParentFile().mkdirs();
                }

                this.converter.convert(inputFile, outputFile);
                flag = true;
                log.info("文件[" + inputFilePath + "]转换为[目标文件：" + outputFile + "]成功！");
            } else {
                log.warn("不支持的转换格式：" + postfix);
            }
        } else {
            log.info("未找到转换的输入文件: " + inputFilePath);
        }

        long end_time = (new Date()).getTime();
        log.info("转换结束，文件转换耗时：[" + (end_time - begin_time) + "]ms");
        return flag;
    }

    public String generateDefaultOutputFilePath(String inputFilePath) {
        String outputFilePath = inputFilePath.replaceAll("." + this.getPostfix(inputFilePath), "_" + this.getPostfix(inputFilePath) + ".pdf");
        return outputFilePath;
    }

    public String getPostfix(String inputFilePath) {
        String[] p = inputFilePath.split("\\.");
        return p.length > 0 ? p[p.length - 1] : null;
    }
}
